import 'package:flutter/material.dart';

/// 时间线事件类型枚举
enum DatelineEventType {
  /// 生日事件
  birthday(1, Icon(Icons.cake), Colors.pink, '生日'),
  /// 游玩事件
  play(2, Icon(Icons.sports_kabaddi), Colors.green, '游玩'),
  /// 看病事件
  medical(3, Icon(Icons.medical_services), Colors.red, '看病'),
  /// 纪念日事件
  anniversary(4, Icon(Icons.favorite), Colors.purple, '纪念日'),
  /// 家庭聚会事件
  familyGathering(5, Icon(Icons.people), Colors.orange, '家庭聚会'),
  /// 学习事件
  study(6, Icon(Icons.school), Colors.blue, '学习'),
  /// 旅行事件
  travel(7, Icon(Icons.flight), Colors.teal, '旅行'),
  /// 美食事件
  food(8, Icon(Icons.restaurant), Colors.amber, '美食'),
  /// 运动事件
  sports(9, Icon(Icons.fitness_center), Colors.indigo, '运动'),
  /// 工作事件
  work(10, Icon(Icons.work), Colors.blueGrey, '工作'),
  /// 购物事件
  shopping(11, Icon(Icons.shopping_bag), Colors.deepPurple, '购物'),
  /// 娱乐事件
  entertainment(12, Icon(Icons.movie), Colors.cyan, '娱乐'),
  /// 节日事件
  festival(13, Icon(Icons.celebration), Colors.deepOrange, '节日'),
  /// 成长记录
  growth(14, Icon(Icons.child_care), Colors.lightGreen, '成长记录'),
  /// 宠物事件
  pet(15, Icon(Icons.pets), Colors.brown, '宠物'),
  /// 家务事件
  housework(16, Icon(Icons.cleaning_services), Colors.grey, '家务'),
  /// 健康事件
  health(17, Icon(Icons.health_and_safety), Colors.lightBlue, '健康'),
  /// 教育事件
  education(18, Icon(Icons.school_outlined), Colors.deepPurple, '教育'),
  /// 其他事件
  other(19, Icon(Icons.event), Colors.grey, '其他');

  /// 事件类型ID（对应后端）
  final int id;
  /// 事件类型对应的图标
  final Icon icon;
  /// 事件类型对应的颜色
  final Color color;
  /// 事件类型名称
  final String displayName;

  const DatelineEventType(this.id, this.icon, this.color, this.displayName);

  /// 根据ID获取事件类型
  static DatelineEventType fromId(int id) {
    return DatelineEventType.values.firstWhere(
      (type) => type.id == id,
      orElse: () => DatelineEventType.other,
    );
  }

  /// 根据ID字符串获取事件类型
  static DatelineEventType fromIdString(String idString) {
    final id = int.tryParse(idString) ?? 19;
    return fromId(id);
  }
}

/// 时间线事件模型
class DatelineEvent {
  /// 事件唯一标识符
  final String? id;
  /// 事件类型ID
  final String eventTypeId;
  /// 事件标题
  final String title;
  /// 事件地点
  final String? location;
  /// 事件时间
  final DateTime eventTime;
  /// 事件描述
  final String? description;
  /// 照片URL列表
  final List<String> photoUrls;

  /// 构造函数
  DatelineEvent({
    this.id,
    required this.eventTypeId,
    required this.title,
    this.location,
    required this.eventTime,
    this.description,
    this.photoUrls = const [],
  });

  /// 获取事件类型枚举
  DatelineEventType get eventType => DatelineEventType.fromIdString(eventTypeId);

  /// 从JSON创建事件对象
  factory DatelineEvent.fromJson(Map<String, dynamic> json) {
    return DatelineEvent(
      id: json['id']?.toString(),
      eventTypeId: json['eventTypeId']?.toString() ?? '19',
      title: json['title'] as String? ?? '',
      location: json['location'] as String?,
      eventTime: json['eventTime'] != null 
          ? DateTime.parse(json['eventTime'] as String)
          : DateTime.now(),
      description: json['description'] as String?,
      photoUrls: json['photoUrls'] != null
          ? List<String>.from(json['photoUrls'] as List)
          : [],
    );
  }

  /// 转换为JSON（用于API请求）
  Map<String, dynamic> toJson() {
    return {
      if (id != null) 'id': id,
      'eventTypeId': eventTypeId,
      'title': title,
      if (location != null) 'location': location,
      'eventTime': eventTime.toIso8601String().replaceAll('T', ' ').substring(0, 19),
      if (description != null) 'description': description,
      'photoUrls': photoUrls,
    };
  }

  /// 转换为API请求格式
  Map<String, dynamic> toApiRequest() {
    return {
      'eventTypeId': eventTypeId,
      'title': title,
      'location': location ?? '',
      'eventTime': eventTime.toIso8601String().replaceAll('T', ' ').substring(0, 19),
      'description': description ?? '',
      'photoUrls': photoUrls,
    };
  }

  /// 创建事件副本并更新指定字段
  DatelineEvent copyWith({
    String? id,
    String? eventTypeId,
    String? title,
    String? location,
    DateTime? eventTime,
    String? description,
    List<String>? photoUrls,
  }) {
    return DatelineEvent(
      id: id ?? this.id,
      eventTypeId: eventTypeId ?? this.eventTypeId,
      title: title ?? this.title,
      location: location ?? this.location,
      eventTime: eventTime ?? this.eventTime,
      description: description ?? this.description,
      photoUrls: photoUrls ?? this.photoUrls,
    );
  }

  @override
  String toString() {
    return 'DatelineEvent{id: $id, eventTypeId: $eventTypeId, title: $title, location: $location, eventTime: $eventTime, description: $description, photoUrls: $photoUrls}';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is DatelineEvent &&
        other.id == id &&
        other.eventTypeId == eventTypeId &&
        other.title == title &&
        other.location == location &&
        other.eventTime == eventTime &&
        other.description == description &&
        other.photoUrls.length == photoUrls.length &&
        other.photoUrls.every((url) => photoUrls.contains(url));
  }

  @override
  int get hashCode {
    return Object.hash(
      id,
      eventTypeId,
      title,
      location,
      eventTime,
      description,
      photoUrls.join(','),
    );
  }
}